package com.canaan.business.service.storehouse.impl;

import java.util.Date;
import java.util.List;
import com.canaan.business.domain.storehouse.StorehouseStockScrap;
import com.canaan.business.mapper.storehouse.StorehouseStockScrapMapper;
import com.canaan.business.service.storehouse.IStorehouseStockScrapService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * 报废仓库存Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-06
 */
@Service
public class StorehouseStockScrapServiceImpl implements IStorehouseStockScrapService {
    @Resource
    private StorehouseStockScrapMapper storehouseStockScrapMapper;

    /**
     * 查询报废仓库存
     *
     * @param id 报废仓库存主键
     * @return 报废仓库存
     */
    @Override
    public StorehouseStockScrap selectStorehouseStockScrapById(Long id) {
        return storehouseStockScrapMapper.selectStorehouseStockScrapById(id);
    }

    /**
     * 查询报废仓库存列表
     *
     * @param storehouseStockScrap 报废仓库存
     * @return 报废仓库存
     */
    @Override
    public List<StorehouseStockScrap> selectStorehouseStockScrapList(StorehouseStockScrap storehouseStockScrap) {
        return storehouseStockScrapMapper.selectStorehouseStockScrapList(storehouseStockScrap);
    }

    /**
     * 新增报废仓库存
     *
     * @param storehouseStockScrap 报废仓库存
     * @return 结果
     */
    @Override
    public int insertStorehouseStockScrap(StorehouseStockScrap storehouseStockScrap) {
        if(StringUtils.isBlank(storehouseStockScrap.getNo())){
            storehouseStockScrap.setNo(TableNoUtil.getNo(StorehouseStockScrap.class));
        }
        storehouseStockScrap.setCreateBy(SecurityUtils.getUsername());
        storehouseStockScrap.setCreateTime(DateUtils.getNowDate());
        return storehouseStockScrapMapper.insertStorehouseStockScrap(storehouseStockScrap);
    }

    /**
     * 批量新增报废仓库存
     *
     * @param storehouseStockScrapList 报废仓库存列表
     * @return 结果
     */
    @Override
    public int batchInsertStorehouseStockScrap(List<StorehouseStockScrap> storehouseStockScrapList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseStockScrap storehouseStockScrap : storehouseStockScrapList){
            if(StringUtils.isBlank(storehouseStockScrap.getNo())){
                storehouseStockScrap.setNo(TableNoUtil.getNo(StorehouseStockScrap.class));
            }
            storehouseStockScrap.setCreateBy(username);
            storehouseStockScrap.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<storehouseStockScrapList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseStockScrapList.size()) {
                endIndex = storehouseStockScrapList.size();
            }
            insertNum = insertNum + storehouseStockScrapMapper.batchInsertStorehouseStockScrap(storehouseStockScrapList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改报废仓库存
     *
     * @param storehouseStockScrap 报废仓库存
     * @return 结果
     */
    @Override
    public int updateStorehouseStockScrap(StorehouseStockScrap storehouseStockScrap) {
        storehouseStockScrap.setUpdateBy(SecurityUtils.getUsername());
        storehouseStockScrap.setUpdateTime(DateUtils.getNowDate());
        return storehouseStockScrapMapper.updateStorehouseStockScrap(storehouseStockScrap);
    }

    /**
     * 批量修改报废仓库存
     *
     * @param storehouseStockScrapList 报废仓库存列表
     * @return 结果
     */
    @Override
    public int batchUpdateStorehouseStockScrap(List<StorehouseStockScrap> storehouseStockScrapList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseStockScrap storehouseStockScrap : storehouseStockScrapList){
            storehouseStockScrap.setUpdateBy(username);
            storehouseStockScrap.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<storehouseStockScrapList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseStockScrapList.size()) {
                endIndex = storehouseStockScrapList.size();
            }
            updateNum = updateNum + storehouseStockScrapMapper.batchUpdateStorehouseStockScrap(storehouseStockScrapList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除报废仓库存
     *
     * @param ids 需要删除的报废仓库存主键集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockScrapByIds(Long[] ids) {
        return storehouseStockScrapMapper.deleteStorehouseStockScrapByIds(ids);
    }

    /**
     * 删除报废仓库存信息
     *
     * @param id 报废仓库存主键
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockScrapById(Long id) {
        return storehouseStockScrapMapper.deleteStorehouseStockScrapById(id);
    }

    /**
     * 查询报废仓库存
     *
     * @param no 编号
     * @return 报废仓库存
     */
    @Override
    public StorehouseStockScrap selectStorehouseStockScrapByNo(String no) {
        return storehouseStockScrapMapper.selectStorehouseStockScrapByNo(no);
    }

    /**
     * 批量删除报废仓库存
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockScrapByNos(String[] nos) {
        return storehouseStockScrapMapper.deleteStorehouseStockScrapByNos(nos);
    }

    /**
     * 删除报废仓库存信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockScrapByNo(String no) {
        return storehouseStockScrapMapper.deleteStorehouseStockScrapByNo(no);
    }

    @Override
    public List<StorehouseStockScrap> selectStorehouseStockScrapList2(StorehouseStockScrap storehouseStockScrap) {
        return storehouseStockScrapMapper.selectStorehouseStockScrapList2(storehouseStockScrap);
    }
}
